﻿using System;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace OneDollarStore.Helpers
{
    public static class HtmlHelpers
    {
        public static string Paging(this HtmlHelper helper, int NumberOfPages, int CurrentPage, int PageSize, string action)
        {
            string prefix = "";
            if (action.EndsWith("&")) // Search URL
            {
                prefix = "";
            }
            else
            {
                prefix = "?";
            }
            string result = "";
            int prev = CurrentPage - 1;
            int next = CurrentPage + 1;
            if (NumberOfPages <= 1)
            {
                return NumberOfPages.ToString();
            }
            else
            {
                for (int i = 1; i < CurrentPage; i++)
                {
                    result += "<a style=\"color:red\" href=\"" + action + prefix + "page=" + i.ToString() + "\">" + i.ToString() + "   " + "</a>";
                }
                result += CurrentPage.ToString() + "   ";
                for (int j = CurrentPage + 1; j <= NumberOfPages; j++)
                {
                    result += "<a style=\"color:red\" href=\"" + action + prefix + "page=" + j.ToString() + "\">" + j.ToString() + "   " + "</a>";
                }
                return result;
            }
        }

        public static MvcHtmlString MenuItem(this HtmlHelper htmlHelper, string text, string action, string controller)
        {
            var li = new TagBuilder("li");
            var routeData = htmlHelper.ViewContext.RouteData;
            var currentAction = routeData.GetRequiredString("action");
            var currentController = routeData.GetRequiredString("controller");
            if (string.Equals(currentAction, action, StringComparison.OrdinalIgnoreCase) &&
                string.Equals(currentController, controller, StringComparison.OrdinalIgnoreCase))
            {
                li.AddCssClass("menu-active");
            }
            li.InnerHtml = htmlHelper.ActionLink(text, action, controller).ToHtmlString();
            return MvcHtmlString.Create(li.ToString());
        }
    }
}